package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("添加分类{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类别不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()),"分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类父级ID不能为空");
            SubjectCategoryBO subjectCategory = SubjectCategoryDTOConverter.INSTANCE.convertDTOToCategoryBo(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategory);
            return Result.success(true);
        }catch (Exception e){
            log.error("添加分类失败{}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("查询分类参数{}",JSON.toJSONString(subjectCategoryDTO));
            }

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToCategoryBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOS = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANCE.
                    convertToCategoryDTOList(subjectCategoryBOS);
            if (log.isInfoEnabled()){
                log.info("查询分类信息{}",JSON.toJSONString(subjectCategoryDTOS));
            }
            return Result.success(subjectCategoryDTOS);
        }catch (Exception e){
               log.info("查询分类异常{}",e.getMessage());
               return Result.error(e.getMessage());
        }
    }

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try{
            if (log.isInfoEnabled()){
                log.info("SubjectCategory.queryCategoryByPrimary.dto:{}",JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"父级id不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类别不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToCategoryBo(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOS = subjectCategoryDomainService.queryCategory(subjectCategoryBO);

            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANCE.
                    convertToCategoryDTOList(subjectCategoryBOS);
            if (log.isInfoEnabled()){
                log.info("查询分类信息{}",JSON.toJSONString(subjectCategoryDTOS));
            }
            return Result.success(subjectCategoryDTOS);
        }catch (Exception e){
            log.info("查询分类异常{}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        Boolean updateRes = false;
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategory.update.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToCategoryBo(subjectCategoryDTO);
            int size = subjectCategoryDomainService.queryCategory(subjectCategoryBO).size();
            if (size == 0){
                throw new RuntimeException("试图更新的数据不存在");
            }
            updateRes = subjectCategoryDomainService.update(subjectCategoryBO);
            return Result.success(updateRes);
        } catch (Exception e) {
            log.info("更新分类异常{}", e.getMessage());
            return Result.error(updateRes);
        }
    }

    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        Boolean deleteFlag = false;
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategory.delete.dto:{}", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(), "id不能为空");
            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToCategoryBo(subjectCategoryDTO);
            int size = subjectCategoryDomainService.queryCategory(subjectCategoryBO).size();
            if (size == 0){
                throw new RuntimeException("试图删除的数据不存在");
            }
            deleteFlag = subjectCategoryDomainService.delete(subjectCategoryBO);
            return Result.success(deleteFlag);
        } catch (Exception e) {
            log.info("删除分类异常{}", e.getMessage());
            return Result.error(deleteFlag);
        }
    }
}
